﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Entities;
using SimpleTaskManager.WebUI.Models;

namespace SimpleTaskManager.WebUI.Controllers
{
    /// <summary>
    /// The controller which takes care about the initial setup steps
    /// </summary>
    public class SetupController : Controller
    {
        private IUserRepository repository;
        private ITaskRepository taskRepository;
        private IPasswordEncrypter passEncrypter;

        /// <summary>
        /// Constructor to initialize the repositories.
        /// </summary>
        /// <param name="uRep">A repository containing the Users</param>
        /// <param name="taskRepository">A repository containing the Tasks</param>
        /// <param name="encrypter">The encrypter which will be used to encrypt passwords</param>
        public SetupController(IUserRepository uRep, ITaskRepository taskRepository, IPasswordEncrypter encrypter)
        {
            repository = uRep;
            this.taskRepository = taskRepository;
            passEncrypter = encrypter;
        }
        
        //
        // GET: /Setup/
        /// <summary>
        /// Redirects to the Initialization page.
        /// </summary>
        /// <returns>A redirect to the Init page.</returns>
        public ActionResult Index()
        {
            return RedirectToAction("Init", new { page = 1 });
        }

        /// <summary>
        /// Handles the setup process for the application
        /// </summary>
        /// <param name="page">The current setup page</param>
        /// <returns>The rendered setup page</returns>
        public ActionResult Init(int page)
        {
            if (page == 1)
                return View();
            else if (page == 2)
                return View("AdminCreated");
            else
                return HttpNotFound();
        }

        /// <summary>
        /// Creates the administrator user after the first setup page is submitted.
        /// </summary>
        /// <param name="model">The SetupModel that originates from the setup form.</param>
        /// <returns>Either a redirect to the next page, or the view showing the validation errors.</returns>
        [HttpPost]
        public ActionResult Init(SetupModel model)
        {
            if (ModelState.IsValid && model.AdminPass != model.AdminPassRep)
            {
                ModelState.AddModelError("AdminPassRep", "Passwords don't match.");
            }
            if (ModelState.IsValid)
            {
                //Set up the Administrator account
                var user = new User();

                user.Login = "admin";
                //set the password supplied on the form (it will be hashed by the repository)
                user.Password = model.AdminPass;

                //Use unknown/default values in the rest of the fields
                user.Address = "unknown";
                user.City = "unknown";
                user.Country = "unknown";
                user.DateOfBirth = new DateTime(1900, 01, 01);
                user.Email = "un@kno.wn";
                user.FirstName = "Administrator";
                user.LastName = "System";
                user.PersonalIdentifier = "unknown";
                user.Position = "unknown";
                user.Role = UserRole.SuperUser;
                user.Status = StatusEnum.Hired;
                user.ZIP = "unknown";

                //Add the user to the repository
                repository.CreateUser(user, passEncrypter);

                //Redirect to the next setup page
                return RedirectToAction("Init", new { page = 2 });
            }

            return View(model);
        }
    }
}
